﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using KPIReport.Library;
using KPIReport.Library.DomainObject.Administration.UserManagement;
using KPIReport.Library.Enum;
using KPIReport.Library.Interfaces;

namespace KPIReport.Library.DataAccess.Administration.UserManagement
{
    public partial class recUserRoleRepo 
    {
        public List<recUserRole> GetListByRoleID(int key)
        {
            List<recUserRole> tt = new List<recUserRole>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT UserRoleID, UserID, RoleID, CreatedDate, CreatedBy FROM recUserRole WHERE RoleID = @RoleID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@RoleID", SqlDbType.Int).Value = key;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recUserRole t = new recUserRole();
                                t.UserRoleID = dr[recUserRole.Fields.UserRoleID] == DBNull.Value ? 0 : int.Parse(dr[recUserRole.Fields.UserRoleID].ToString());
                                t.UserID = dr[recUserRole.Fields.UserID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserRole.Fields.UserID].ToString());
                                t.RoleID = dr[recUserRole.Fields.RoleID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserRole.Fields.RoleID].ToString());
                                t.CreatedDate = dr[recUserRole.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recUserRole.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recUserRole.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recUserRole.Fields.CreatedBy].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<recUserRole> GetListByUserID(int key)
        {
            List<recUserRole> tt = new List<recUserRole>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT UserRoleID, UserID, RoleID, CreatedDate, CreatedBy FROM recUserRole WHERE UserID = @UserID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = key;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recUserRole t = new recUserRole();
                                t.UserRoleID = dr[recUserRole.Fields.UserRoleID] == DBNull.Value ? 0 : int.Parse(dr[recUserRole.Fields.UserRoleID].ToString());
                                t.UserID = dr[recUserRole.Fields.UserID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserRole.Fields.UserID].ToString());
                                t.RoleID = dr[recUserRole.Fields.RoleID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserRole.Fields.RoleID].ToString());
                                t.CreatedDate = dr[recUserRole.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recUserRole.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recUserRole.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recUserRole.Fields.CreatedBy].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public recUserRole GetByUserId(int key)
        {
            recUserRole t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT UserRoleID, UserID, RoleID, CreatedDate, CreatedBy FROM recUserRole WHERE UserID = @UserID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = key;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new recUserRole();
                                t.UserRoleID = dr[recUserRole.Fields.UserRoleID] == DBNull.Value ? 0 : int.Parse(dr[recUserRole.Fields.UserRoleID].ToString());
                                t.UserID = dr[recUserRole.Fields.UserID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserRole.Fields.UserID].ToString());
                                t.RoleID = dr[recUserRole.Fields.RoleID] == DBNull.Value ? (int?)null : int.Parse(dr[recUserRole.Fields.RoleID].ToString());
                                t.CreatedDate = dr[recUserRole.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recUserRole.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recUserRole.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recUserRole.Fields.CreatedBy].ToString();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }
    
    }

}
